/******************************************************************************/
/*                                                                            */
/*  Copyright (c) 2009: ngi-central.org                                       */
/*  All rights reserved.                                                      */
/*                                                                            */
/******************************************************************************/


/// \file
/// This file implements the hyper_sphere geometric primitive.

#ifndef __sgl_hyper_sphere_h__
#define __sgl_hyper_sphere_h__

#include <sgl/global_settings.h>
#include <sgl/conditions.h>
#include <sgl/geometric_primitive.h>
#include <sgl/point.h>
#include <sgl/vector.h>
#include <boost/static_assert.hpp>
#include <boost/operators.hpp>
#include <boost/concept_check.hpp>

namespace sgl {


/******************************************************************************/
/*                                                                            */
/* hyper_sphere                                                               */
/*                                                                            */
/******************************************************************************/

template <
	/// The dimensionality. 
	//!
	//! This defaults to two dimensions.
	size_t D=2,
	/// The element type. 
	//! 
	//! This type defaults to SGL_FPTYPE (which in turn defaults to double), 
	//! but any other numeric type is also suitable.
	typename T=SGL_FPTYPE>
/// A hyper_sphere in euclidean space.
//!
//! A hyper_sphere has an origin and a scalar radius.
//!
//! A hyper_sphere in 2D is a circle or a disk. A hyper_shpere in 3D is a sphere 
//! or a ball.
class hyper_sphere
	/// A hyper_sphere is a geometric primitive.
	: public geometric_primitive<D,T>
	, boost::equality_comparable<sgl::hyper_sphere<D,T>, 
		sgl::hyper_sphere<D,T>
	, boost::additive<sgl::hyper_sphere<D,T>, sgl::vector<D,T>
	, boost::multiplicative<sgl::hyper_sphere<D,T>, T
	> > >
{
	// A hyper_sphere must have two or more dimensions.
	BOOST_STATIC_ASSERT(D >= 2);

public:
	/// Default constructor.
	//!
	//! Constructs a zero hyper_sphere.
	hyper_sphere()
		: origin_()
		, radius_()
	{ }

	/// Construct a hyper_sphere from a point and a vector.
	hyper_sphere(
		/// The origin of the hyper_sphere.
		sgl::point<D,T> origin,
		/// The radius of the hyper_sphere.
		T radius)
		: origin_(origin)
		, radius_(radius)
	{ }

	/// Copy constructor.
	hyper_sphere(
		/// The right hand side.
		sgl::hyper_sphere<D,T> const& rhs)
		: origin_(rhs.origin_)
		, radius_(rhs.radius_)
	{ }

	/// Copy assignment operator.
	sgl::hyper_sphere<D,T> & operator = (
		/// The right hand side.
		sgl::hyper_sphere<D,T> rhs)
	{
		swap(rhs);
		return *this;
	}

	/// Exchanges the elements of two hyper_sphere objects.
	void swap(
		/// The other hyper_sphere.
		sgl::hyper_sphere<D,T> & rhs)
	{
		SGL_PRECONDITION(D == rhs.dimensions(),
			"dimension mismatch.");

		std::swap(origin_, rhs.origin_);
		std::swap(radius_, rhs.radius_);
	}

	/// Find the type of the geometric primitive.
	//!
	//! /return The type of the primitive.
	geometric_type type() const
	{
		return sgl::geometric_primitive<D,T>::hyper_sphere;
	} 

	/// Find the dimensionality of the hyper_sphere.
	//!
	//! /return The dimensionality of the hyper_sphere.
	size_t dimensions() const
	{
		return D;
	}

	/// Access the origin of the hyper_sphere.
	//!
	//! /return Returns a reference to the origin of the hyper_sphere.
	sgl::point<D,T> & origin()
	{
		return origin_;
	}

	/// Access the origin of the hyper_sphere.
	//!
	//! /return Returns a const reference to the origin of the hyper_sphere.
	sgl::point<D,T> const & origin() const
	{
		return origin_;
	}

	/// Access the radius of the hyper_sphere.
	//!
	//! /return Returns a reference to the radius of the hyper_sphere.
	T & radius()
	{
		return radius_;
	}

	/// Access the radius of the hyper_sphere.
	//!
	//! /return Returns a const reference to the radius of the hyper_sphere.
	T const & radius() const
	{
		return radius_;
	}

	/// Equality operator.
	//!
	//! Two hyper_spheres are compared for equality by comparing their 
	//! origin and radius in a pairwise manner. The hyper_spheres are equal 
	//! only if all pairwise comparisons are equal.
	//!
	//! Through the use of boost::equality as a base class operator != is 
	//! defined as well and implemented in terms of this operator ==.
	//!
	//! \return true if equal, false otherwise.
    bool operator == (
		/// Right hand side.
		sgl::hyper_sphere<D,T> const& rhs) const
    {
		SGL_PRECONDITION(D == rhs.dimensions(),
			"dimension mismatch.");

		return 
			origin_ == rhs.origin_ &&
			radius_ == rhs.radius_;
    }

	/// Add a vector to a hyper_sphere.
	//!
	//! Adding a vector to a hyper_sphere moves the hyper_sphere in 
	//! direction of the vector. Addition is implemented by pairwise addition of 
	//! the components of the hyper_sphere's origin and the vector.
	//!
	//! Through the use of boost::additive as a base class operator + is 
	//! defined as well and implemented in terms of this operator +=.
	//!
	//! \return A reference to this object.
    sgl::hyper_sphere<D,T> & operator += (
		/// Right hand side.
		sgl::vector<D,T> const& rhs)
    {
		SGL_PRECONDITION(D == rhs.dimensions(),
			"dimension mismatch.");

		origin_ += rhs;

        return *this;
    }

	/// Subtract a vector from a hyper_sphere.
	//!
	//! Subtracting a vector from a hyper_sphere moves the hyper_sphere in 
	//! the opposite direction of the vector. Subtraction is implemented by 
	//! pairwise subtraction of the components of the hyper_sphere's origin 
	//! and the vector.
	//!
	//! Through the use of boost::additive as a base class operator - is 
	//! defined as well and implemented in terms of this operator -=.
	//!
	//! \return A reference to this object.
    sgl::hyper_sphere<D,T> & operator -= (
		/// Right hand side.
		sgl::vector<D,T> const& rhs)
    {
		SGL_PRECONDITION(D == rhs.dimensions(),
			"dimension mismatch.");

		origin_ -= rhs;

		return *this;
    }

	/// Scale a hyper_sphere by multiplication.
	//!
	//! A hyper_sphere is scaled by multiplying its radius with the factor.
	//!
	//! Through the use of boost::multiplicative as a base class operator * is 
	//! defined as well and implemented in terms of this operator *=.
	//!
	//! \return A reference to this object.
	sgl::hyper_sphere<D,T> & operator *= (
		/// The scaling factor.
		T const& factor)
	{
		radius_ *= factor;

		return *this;
	}

	/// Scale a hyper_sphere by division.
	//!
	//! A hyper_sphere is scaled by dividing its radius through the divisor.
	//!
	//! Through the use of boost::multiplicative as a base class operator / is 
	//! defined as well and implemented in terms of this operator /=.
	//!
	//! \return A reference to this object.
	sgl::hyper_sphere<D,T> & operator /= (
		/// The scaling factor.
		T const& divisor)
	{
		radius_ /= divisor;

		return *this;
	}

private:
	// The origin of the hyper_sphere.
	sgl::point<D,T> origin_;

	/// The radius of the hyper_sphere.
	T radius_;
};


/// A two-dimensional ellipse with the default component type.
typedef sgl::hyper_sphere<2> circle2;

/// A two-dimensional hyper_sphere with float component type.
typedef sgl::hyper_sphere<2, float> circle2f;

/// A two-dimensional hyper_sphere with double component type.
typedef sgl::hyper_sphere<2, double> circle2d;


/// A three-dimensional sphere with the default component type.
typedef sgl::hyper_sphere<3> sphere3;

/// A three-dimensional sphere with float component type.
typedef sgl::hyper_sphere<3, float> sphere3f;

/// A three-dimensional sphere with double component type.
typedef sgl::hyper_sphere<3, double> sphere3d;


} // namespace sgl

#endif // __sgl___sgl_hyper_sphere_h___h__
